<template>
  <el-container class="home_container">
    <!-- //头部区域 -->
    <el-header :style="{ backgroundColor: $store.state.color }">
      <!-- <img src="../assets/heima.png" alt="" /> -->
      <div class="box">
        <div class="toggle_button" @click="toggleCollapse">
          <i v-if="!isCollapse" class="el-icon-s-fold"></i>
          <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="title">后台管理系统</div>
      </div>
      <div class="header-right">
        <div class="header-user-con">
          <!-- 颜色选择 -->
          <el-color-picker
            v-model="color"
            size="small"
            show-alpha
            :predefine="predefineColors"
            @change="changeColor"
          ></el-color-picker>
          <!-- 全屏显示 -->
          <div class="btn-fullscreen" @click="handleFullScreen">
            <el-tooltip
              effect="dark"
              :content="fullscreen ? `取消全屏` : `全屏`"
              placement="bottom"
            >
              <i class="el-icon-rank"></i>
            </el-tooltip>
          </div>
          <!-- 消息中心 -->
          <div class="btn-bell">
            <el-tooltip
              effect="dark"
              :content="message ? `有${message}条未读消息` : `消息中心`"
              placement="bottom"
            >
              <router-link to="/tabs">
                <i class="el-icon-bell"></i>
              </router-link>
            </el-tooltip>
            <span class="btn-bell-badge" v-if="message"></span>
          </div>
          <!-- 用户头像 -->
          <!-- <div class="user-avator">
            <img src="../assets/img/tuzi.gif" />
          </div> -->
          <!-- 用户名下拉菜单 -->
          <!-- <el-dropdown
            size="small"
            class="user-name"
            trigger="click"
            @command="handleCommand"
          >
            <span class="el-dropdown-link">
              {{ username }}
              <i class="el-icon-caret-bottom"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <a href="https://www.baidu.com">
                <el-dropdown-item>项目仓库</el-dropdown-item>
              </a>
              <el-dropdown-item divided command="loginout"
                >退出登录</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown> -->

          <el-popover placement="bottom" width="250" trigger="click">
            <user-box></user-box>
            <span class="user-avator" slot="reference">
              <img src="../assets/img/tuzi.gif" />
              {{ username }}
            </span>
          </el-popover>
        </div>
      </div>
    </el-header>
    <!-- //页面主体区域 -->
    <el-container>
      <!-- //侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- //侧边栏菜单区域 -->
        <el-menu
          background-color="#fff"
          text-color="#000"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- //右侧内容主体 -->
      <el-main>
        <div class="content-box" :class="{ 'content-collapse': collapse }">
          <!-- <v-tags></v-tags> -->
          <div class="content">
            <transition name="move" mode="out-in">
              <router-view></router-view>
            </transition>
            <el-backtop target=".content"></el-backtop>
          </div>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import bus from '../components/common/bus.js'
import UserBox from '../components/common/UserBox.vue'
// import { mapState } from 'vuex'
export default {
  components: {
    UserBox
  },
  data() {
    return {
      predefineColors: [
        '#ff4500',
        '#ff8c00',
        '#ffd700',
        '#90ee90',
        '#00ced1',
        '#1e90ff',
        '#c71585',
        'rgba(255, 69, 0, 0.68)',
        'rgb(255, 120, 0)',
        'hsv(51, 100, 98)',
        'hsva(120, 40, 94, 0.5)',
        'hsl(181, 100%, 37%)',
        'hsla(209, 100%, 56%, 0.73)',
        '#242f42'
      ],
      form: {
        id: '',
        authName: '',
        path: ''
      },
      dialogVisible: false,
      uploadVisible: false,
      tagsList: [],
      collapse: false,
      message: 2,
      fullscreen: false,
      menulist: [
        {
          id: 125,
          authName: '用户管理',
          path: 'users',
          children: [
            {
              id: 110,
              authName: '用户列表',
              path: 'users',
              children: [],
              order: null
            }
          ],
          order: 1
        },
        {
          id: 103,
          authName: '权限管理',
          path: 'rights',
          children: [
            {
              id: 111,
              authName: '角色列表',
              path: 'roles',
              children: [],
              order: null
            },
            {
              id: 112,
              authName: '权限列表',
              path: 'rights',
              children: [],
              order: null
            }
          ],
          order: 2
        },
        {
          id: 101,
          authName: '商品管理',
          path: 'goods',
          children: [
            {
              id: 104,
              authName: '商品列表',
              path: 'goods',
              children: [],
              order: 1
            },
            {
              id: 115,
              authName: '分类参数',
              path: 'params',
              children: [],
              order: 2
            },
            {
              id: 121,
              authName: '商品分类',
              path: 'categories',
              children: [],
              order: 3
            }
          ],
          order: 3
        },
        {
          id: 102,
          authName: '订单管理',
          path: 'orders',
          children: [
            {
              id: 107,
              authName: '订单列表',
              path: 'orders',
              children: [],
              order: null
            }
          ],
          order: 4
        },
        {
          id: 145,
          authName: '系统设置',
          path: 'reports',
          children: [
            {
              id: 146,
              authName: '菜单管理',
              path: 'reports',
              children: [],
              order: null
            }
          ],
          order: 5
        }
      ],
      iconsObj: {
        125: 'fa fa-users',
        103: 'fa fa-cube',
        101: 'fa fa-shopping-bag',
        102: 'fa fa-file',
        145: 'fa fa-cogs'
      },
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      dialogImageUrl: '',
      disabled: false,
      uploadFile: {
        dialogVisible: false,
        uploadImageList: [],
        uploadMedia: []
      },
      imgPath: '',
      flag: true
    }
  },
  computed: {
    // ...mapState(['color']),

    username() {
      const username = window.sessionStorage.getItem('username')
      return username
    }
  },
  watch: {
    // color(n, o) {
    //   window.localStorage.setItem('BGcolor', this.color)
    // }
  },
  created() {
    // this.getMenuList()
    this.color = window.localStorage.getItem('BGcolor')
    this.activePath = window.sessionStorage.getItem('activePath')
    // 只有在标签页列表里的页面才使用keep-alive，即关闭标签之后就不保存到内存中了。
    bus.$on('tags', msg => {
      const arr = []
      for (let i = 0, len = msg.length; i < len; i++) {
        msg[i].name && arr.push(msg[i].name)
      }
      this.tagsList = arr
    })
  },
  methods: {
    // 用户名下拉菜单选择事件
    handleCommand(command) {
      if (command === 'loginout') {
        window.sessionStorage.clear()
        this.$router.push('/login')
      }
    },
    // 全屏事件
    handleFullScreen() {
      const element = document.documentElement
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen()
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen()
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen()
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen()
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen()
        }
      }
      this.fullscreen = !this.fullscreen
    },
    // 获取所有的菜单
    // async getMenuList() {
    //   const { data: res } = await this.$http.get('menus')
    //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    //   this.menulist = res.data
    //   console.log(res.data)
    // },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 切换颜色
    changeColor(v) {
      this.$store.commit('toggleColor', v)
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  // background-color: #333744;
  background-color: #fff;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
  // padding: 20px;
}
.home_container {
  width: 100%;
  height: 100%;
}
.fa {
  margin-right: 10px;
}
.box {
  height: 100%;
  width: 200px;
}
.toggle_button {
  width: 64px;
  height: 100%;
  font-size: 22px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.title {
  height: 100%;
  line-height: 60px;
}
.header-user-con {
  display: flex;
  height: 60px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
  margin-left: 10px;
}
.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}
.btn-bell-badge {
  position: absolute;
  right: 0;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
  color: #fff;
}
.btn-bell .el-icon-bell {
  color: #fff;
}
.user-name {
  margin-left: 10px;
}
.user-avator {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60px;
  margin-left: 20px;
  cursor: pointer;
}
.user-avator:hover {
  background-color: rgb(10, 110, 153);
}
.user-avator img {
  margin-right: 10px;
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
.box {
  width: 250px;
}
</style>
